/*
 * EditSetup.java
 *
 * Created on 28 luty 2007, 22:34
 *
 */

package arocms.links;

import arocms.Constans;
import arocms.business.Link;
import arocms.business.LinkDAO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 *
 * @author arek
 */
public class EditSetup extends Action{
    
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private final Logger log = Logger.getLogger(this.getClass());
    
    
    /** Creates a new instance of EditSetup */
    public EditSetup() {
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    //get an id of a Link to edit
        String id = request.getParameter("id");
        log.debug("--> execute");
        if (id==null) {
            log.debug("<-- execute");
            return mapping.findForward(FAILURE);
        }
        Long pk = new Long(id);
        
        //get this link from database
        Link aLink = new LinkDAO().find(pk);
        log.debug("execute " + aLink);
        AddLinkForm aForm = new AddLinkForm();
        //copy properties to a form bean
        aForm.setAddress(aLink.getAddress());
        aForm.setDescription(aLink.getDescription());
        aForm.setId(aLink.getId());
        aForm.setTitle(aLink.getTitle());
        //set a form bean in a request or session, so that form fields would be automatically populated
        String scope = mapping.getScope();
        if (scope.equals("session")) request.getSession().setAttribute(mapping.getAttribute(),aForm);
        else request.setAttribute(mapping.getAttribute(),aForm);
        log.debug("<- execute");
        return mapping.findForward(SUCCESS);
    }
    
}
